package com.white.commonlib.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.white.commonlib.engine.TextImgMixer.ImgGetter;


/**
 * Created by Administrator on 2017/5/4 0004.
 */

public class WordImgChaosTextView extends AppCompatTextView implements ViewTreeObserver.OnGlobalLayoutListener
{
    public WordImgChaosTextView(Context context)
    {
        this(context, null);
    }

    public WordImgChaosTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public WordImgChaosTextView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        imgGetter = new ImgGetter(this, getContext());
    }

    ImgGetter imgGetter;
    SpannableStringBuilder ssb;
    ImageSpan[] imgSpans;
    String[] imgUrls;
    String str;

    private boolean contentHasSetted;
    private boolean needBloackImgClickEvent;
    private boolean needRestoreOnAttach;

    public void setneedRestoreOnAttach(boolean yn)
    {
        needRestoreOnAttach = yn;
    }

    public void blockImgclick(boolean yn)
    {
        needBloackImgClickEvent = yn;
    }

    public void setHtmlFromString(String html)
    {
        str = html;
        setContent();
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu)
    {
        super.onCreateContextMenu(null);
    }

    private void setImgClick(SpannableStringBuilder ssb)
    {
        if(lis != null)
        {
            imgSpans = ssb.getSpans(0, ssb.length(), ImageSpan.class);
            if(imgSpans != null)
            {
                imgUrls = new String[imgSpans.length];
                for(int i = 0;i<imgSpans.length;i++)
                {
                    imgUrls[i] = imgSpans[i].getSource();
                }
                for (int i = 0; i < imgSpans.length; i++)
                {
                    //获取图片span的起尾
                    int start = ssb.getSpanStart(imgSpans[i]);
                    int end = ssb.getSpanEnd(imgSpans[i]);
                    final int finalI = i;

                    //将span转化为clickspan
                    ssb.setSpan(new ClickableSpan()
                    {
                        @Override
                        public void onClick(View widget)
                        {
                            lis.clicked(imgUrls[finalI]);
                        }
                    }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
    }

    public void setOnImgClickListener(OnImgClickListener a)
    {
        lis = a;
    }

    private OnImgClickListener lis;
    public interface OnImgClickListener
    {
        void clicked(String imgUrl);
    }

    @Override
    protected void onDetachedFromWindow()
    {
        if(imgGetter != null)
        {
            imgGetter.recycle();
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void onAttachedToWindow()
    {
        super.onAttachedToWindow();
        getViewTreeObserver().addOnGlobalLayoutListener(this);
        //需要恢复时才恢复
        if(needRestoreOnAttach)
        {
            setContent();
        }
    }

    int width;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        if(imgGetter != null)
        {
            imgGetter.setContainerWidth(width);
        }
        setContent();
    }

    @Override
    public void onGlobalLayout()
    {
        getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }

    private void setContent()
    {
        if(imgGetter != null)
        {
            if(!TextUtils.isEmpty(str))
            {
                setText(str);
                contentHasSetted = true;
                ssb = (SpannableStringBuilder) Html.fromHtml(str, imgGetter, null);
                if (!needBloackImgClickEvent)
                {
                    setImgClick(ssb);
                }
                setText(ssb);
                if (!needBloackImgClickEvent)
                {
                    setMovementMethod(LinkMovementMethod.getInstance());
                }
//                setTextColor(getResources().getColor(R.color.text_black));
            }
        }
    }
}
